package com.jinlejia.feedback.question.adapter.question;

import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.jinlejia.feedback.R;
import com.jinlejia.feedback.databinding.FbAdapterQuestionDetailsListBinding;
import com.jinlejia.feedback.question.http.QuestionDetailsHelper;

import java.util.ArrayList;
import java.util.Map;

/**
 * 问题回复列表
 */

public class QuestionListHolderHelper {

    public static void bindViewHolder(final KeyHelper.EventListener eventListener, RecyclerView.Adapter adapter, ViewHolder holder, ArrayList<Map<String, Object>> mData, final int position){
        final FbAdapterQuestionDetailsListBinding binding = DataBindingUtil.getBinding(holder.itemView);
        final Map<String, Object> map = mData.get(position);
        boolean isTop = Boolean.parseBoolean(String.valueOf(map.get("isTop")));
        if (isTop){ //是否置顶
            binding.stickTextView.setVisibility(View.VISIBLE);
        }else {
            binding.stickTextView.setVisibility(View.INVISIBLE);
        }
        binding.refreshButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                eventListener.onSearchReply();
            }
        });
        binding.replyInfoTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String replyCode = String.valueOf(map.get("replyCode"));
                String replyUserId = String.valueOf(map.get("replyUserId"));
                String replyUserName = String.valueOf(map.get("replyUserName"));
                eventListener.onReplyComment(replyCode,replyUserId,replyUserName,position);
            }
        });
        int lastPosition = position-1;
        if (lastPosition>0 && mData.size()>lastPosition ){
            Map<String, Object> lastOneMap = mData.get(lastPosition);
            if (!"5".equals(String.valueOf(lastOneMap.get("type")))){ //如果是第一个
                binding.titleTextView.setVisibility(View.VISIBLE);
                binding.refreshButton.setVisibility(View.VISIBLE);
            }else {
                binding.titleTextView.setVisibility(View.GONE);
                binding.refreshButton.setVisibility(View.GONE);
            }
        }
        if (mData.size()==position+1){ //如果是最后一个
            binding.lineView.setVisibility(View.GONE);
        }else {
            binding.lineView.setVisibility(View.VISIBLE);
        }
        //用户头像
        Glide.with(binding.userIconImageView.getContext())
                .load(String.valueOf(map.get("replyUserPortrait")))
                .placeholder(R.mipmap.jlj_role_head)
                .error(R.mipmap.jlj_role_head)
                .into(binding.userIconImageView);

        //用户名称
        binding.userNameTextView.setText(String.valueOf(map.get("replyUserName")));
        //用户部门 or  职务
        String userDuty = String.valueOf(map.get("replyUserDuty"));
        String userInfo = String.valueOf(userDuty); //用户职务
        String userDeptStr = String.valueOf(map.get("replyUserDept")); //用户部门
        if (!"null".equalsIgnoreCase(userDeptStr) && !TextUtils.isEmpty(userDeptStr)){
            //如果部门为null 则显示职务
            userInfo = userDeptStr;
        }
        binding.userInfoTextView.setText(userInfo);
        //回复时间
        binding.replyTimeTextView.setText(String.valueOf(map.get("replyTime")));
        //回复内容
        binding.replyInfoTextView.setText(String.valueOf(map.get("replyContent")));
        //回复个数
        int replyCommentCount = 0;
        try{
            replyCommentCount = Integer.parseInt(String.valueOf(map.get("replyCommentCount")));
        }catch (Exception ignored){
        }
        if (replyCommentCount==0){
            binding.flowAllCountView.setVisibility(View.GONE);
        }else {
            binding.flowAllCountView.setTypeCount(replyCommentCount);
            binding.flowAllCountView.setVisibility(View.VISIBLE);
        }
        binding.flowAllCountView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                eventListener.onToAllReplyActivity(String.valueOf(map.get("replyCode")));
            }
        });

        //region 点赞操作
        final String likeStateKey = "replyLikeState";
        final String likeCountKey = "replyLikeCount";
        int likeCount;
        try{
            likeCount = Integer.parseInt(String.valueOf(map.get(likeCountKey)));//收藏数量
        }catch (Exception e){
            likeCount = 0;
        }
        boolean isLike = (boolean) map.get(likeStateKey);
        binding.likeCountTextView.setText(String.valueOf(likeCount));
        if (isLike){
            binding.likeStateImageView.setImageResource(R.mipmap.fb_like_icon);
            binding.likeCountTextView.setTextColor(Color.parseColor("#FCCB00"));
        }else {
            binding.likeStateImageView.setImageResource(R.mipmap.fb_unlike_icon);
            binding.likeCountTextView.setTextColor(Color.parseColor("#999999"));
        }

        View.OnClickListener likeListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = Integer.parseInt(binding.likeCountTextView.getText().toString());
                boolean isLike = (boolean) map.get(likeStateKey);
                if (isLike){
                    binding.likeStateImageView.setImageResource(R.mipmap.fb_unlike_icon);
                    binding.likeCountTextView.setTextColor(Color.parseColor("#999999"));
                    count = count - 1;
                }else {
                    binding.likeStateImageView.setImageResource(R.mipmap.fb_like_icon);
                    binding.likeCountTextView.setTextColor(Color.parseColor("#FCCB00"));
                    count = count + 1;
                }
                binding.likeCountTextView.setText(String.valueOf(count));
                eventListener.onLikeOperation(isLike,String.valueOf(map.get("replyCode")), QuestionDetailsHelper.LikeType.Reply,map,position);
                map.put(likeStateKey,!isLike);
                map.put(likeCountKey,count);
            }
        };

        binding.likeOperateLayout.setOnClickListener(likeListener);
        //endregion
    }


    public static class ViewHolder extends RecyclerView.ViewHolder{

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
}
